package com.huayal.mall.coupon.service.impl;

import com.huayal.common.vo.MemberPrice;
import com.huayal.common.vo.SkuReductionDTO;
import com.huayal.mall.coupon.entity.MemberPriceEntity;
import com.huayal.mall.coupon.entity.SkuLadderEntity;
import com.huayal.mall.coupon.service.MemberPriceService;
import com.huayal.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huayal.common.utils.PageUtils;
import com.huayal.common.utils.Query;

import com.huayal.mall.coupon.dao.SkuFullReductionDao;
import com.huayal.mall.coupon.entity.SkuFullReductionEntity;
import com.huayal.mall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    //处理满减、折扣、会员价信息
    @Transactional
    @Override
    public void saveReductionInfo(SkuReductionDTO skuReductionDTO) {
        //5.3 保存满减、折扣、会员价信息 mall-sms：sms_sku_ladder sms_sku_full_reduction sms_member_price
        //1，折扣
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionDTO,skuLadderEntity);
        skuLadderEntity.setAddOther(1);
        skuLadderService.save(skuLadderEntity);
        //2，满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionDTO,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(1);
        this.save(skuFullReductionEntity);
        //3，会员价
        List<MemberPrice> memberPrices = skuReductionDTO.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrices.stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
//            BeanUtils.copyProperties(memberPrice,memberPriceEntity);
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());

            memberPriceEntity.setSkuId(skuReductionDTO.getSkuId());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}